//
//  SetViewController.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 王丽珍. All rights reserved.
//

#import "SetViewController.h"
#import "RESideMenu.h"
#import "UMSocial.h"
#import "GeneralSettingsViewController.h"
@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate,UMSocialUIDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;


- (IBAction)back:(UIBarButtonItem *)sender;


@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadTableView];
    
    
}

#pragma mark --- tableView部分
- (void)loadTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    self.navigationController.navigationBar.translucent = NO;
    _dataArray = [[NSMutableArray alloc] initWithObjects:@"通用设置",@"分享时光",@"关于我们",@"退出登录", nil];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text = _dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:
        {
            [self loadExitView];
        }
            break;
        case 1:
        {
            [self loadShaer];
        }
            break;
            
        case 3:
        {
            [self loadExit];
        }
            break;
            
        default:
            break;
    }
    
}

#pragma maek --- 友盟第三方分享
- (void)loadShaer{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([user boolForKey:@"isLogin"]) {
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"5645e4d367e58e45cb000e7e"
                                          shareText:@""
                                         shareImage:[UIImage imageNamed:@"icon.png"]
                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToRenren,UMShareToDouban,nil]
                                           delegate:self];

    }else{
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请您登陆哦" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alertView show];
        
        
    }
    
}

#pragma mark --- 友盟第三方退出登陆
- (void)loadExit{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    if ([user boolForKey:@"isLogin"]) {
        
        [[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToSina  completion:^(UMSocialResponseEntity *response){
            NSLog(@"response is %@",response);
        }];
        [user setBool:NO forKey:@"isLogin"];
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"退出成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        
        // 发一个通知出去让前面的页面改变头像和昵称
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dataBase" object:nil];
        
    }else{
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请您登陆哦" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alertView show];
        
        
    }
    
    

}


//#pragma mark --- 跳往通用页面
- (void)loadExitView{
    UIStoryboard *storBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    GeneralSettingsViewController *gensetVC = [storBoard instantiateViewControllerWithIdentifier:@"genVC"];
    [self.navigationController pushViewController:gensetVC animated:YES];
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)back:(UIBarButtonItem *)sender {
    
    [self.sideMenuViewController presentLeftMenuViewController];
}
@end
